package mycomp.chlor;

import scala.Option;
import scaladuct.First;
import scaladuct.Property;
import scaladuct.Ref;
import scaladuct.Starter;
import scaladuct.l10n.HasHintsMetaData;
import scaladuct.l10n.HasHintsMetaDataSource;
import scaladuct.l10n.HasHintsMetaDataSourceProvider;
import scaladuct.hint.Help;
import scaladuct.l10n.DefaultCommonMetaData;
import scaladuct.lists.ListForProperty;
import scaladuct.lists.ListsManager;
import scaladuct.lists.ListsSource;

import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * <p/>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Feb 23, 2011
 * Time: 3:30:41 PM
 */
public class TwoRefStarter implements Starter<String>, ListsSource, HasHintsMetaDataSourceProvider,
        Serializable {
//    @Override
//    public Option<ListsManager> listsForPhase(String phase) {
//        return None;
//    }

    public void testMethod(List<String> b) {

    }

    public String newInstance() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("Value2");
        list.add("Value3");
        return phaseOne(120, list, true, NiceEnum1.NiceValue1, "", "");
    }

    @Help("phaseOne")
    public String phaseOne(@Min(100) @Help int a,
                           @Size(min = 1, max = 10) List<String> b,
                           boolean c,
                           NiceEnum1 d,
                           @Ref(name = "ref1") String refString1,
                           @Ref(name = "ref2") String refString2) {
        System.out.println("TwoReferencesStarterInJava:b:" + b);
        return phaseTwo(a, refString1, refString2, true);
    }

    public void unphaseOne() {
        System.out.println("unphaseOne");
    }

    @Help
    public String phaseTwo(int a, String refString1, String refString2, @First boolean reverse) {
        if (reverse)
            return "!" + refString2 + refString1 + a;
        else
            return "" + a + refString1 + refString2 + "!";
    }

    public void unphaseTwo() {
        System.out.println("unphaseTwo");
    }

    public void destroy(String instance) {
    }

    public Option<ListsManager> listsForPhase(String phase) {
        if ("phaseOne".equals(phase)) {
            ListsManager lm = new ListsManager() {
                public Option<ListForProperty<Object>> listForProperty(Property<Object> property) {
                    if ("parameter1".equals(property.name())) {
                        //new String[] {"Value1", "Value2", "Value3"};
                        List values = new ArrayList();
                        Collections.addAll(values, "Value1", "Value2", "Value3");
                        return Option.apply(new ListForProperty<Object>(property, values, true, true));
                        //return null;
                    }
                    return Option.apply(null);
                }
            };
            return Option.apply(lm);
        }
        return Option.apply(null);
    }

    public Option configPageMetaDataSource(String phaseMethod) {
        return Option.apply(new MyConfigPageMetaDataSource());
    }

    class MyConfigPageMetaDataSource implements HasHintsMetaDataSource {
        public Option configPageMetaData(final Locale loc) {
            class MyConfigPageMetaData implements HasHintsMetaData {
//                Locale pageLocale = loc;
//                Option pageMeta = Option.apply(new DefaultCommonMetaData(Option.apply("Titulek"),
//                        Option.apply("Phase 1 Description"),
//                        Option.apply(null)));


                public Locale pageLocale() {
                    return loc;
                }

                public Option pageMeta() {
                    return Option.apply(new DefaultCommonMetaData((Option<String>) Option.apply("Titulek"),
                        (Option<String>)Option.apply("Phase 1 Description"),
                        (Option<URL>) Option.apply((URL) null)));
                }

                public Option propertyMeta(Property property) {
                    if ("parameter0".equals(property)) {
                        return Option.apply(new DefaultCommonMetaData((Option<String>) Option.apply("Titulek"),
                                (Option<String>) Option.apply("Popisek"), (Option<URL>) Option.apply((URL)null)));
                    }
                    return Option.apply(null);
                }

                public Option propertyValueMeta(Property property) {
                    return Option.apply(null);
                }

                public Option hintMeta(Property hint) {
                    return Option.apply(null);
                }

            }

            return Option.apply(new MyConfigPageMetaData());
        }
    }

    //
//  def listsForPhase(phase: String) = phase match  {
//    case "phaseOne" => {
//      Some(new ListsManager {
//        def scalaListForProperty(property: Property[AnyRef]) = property.name match {
//          case "parameter1" => {
//            Some(new ListForProperty(property, List("Value1", "Value2", "Value3"), true))
//          }
//          case _ => None
//        }
//      })
//    }
//    case _ => None
//  }

}

